<template>
	<view class="home">
		<custom-navbar title="我的反馈" :show-back="true" @back="handleBack" />

		<view class="content" :style="{ paddingTop: navBarHeight + 'px' }">
			<!-- 分类选项卡 -->
			<view class="search p0030">
				<view class="flex">
					<view v-for='(item,index) in feedbackTypes' :key="index" class="typelist fcolcc"
						:style="{ marginRight: index < feedbackTypes.length - 1 ? '67rpx' : '0' }">
						<view class="f30bc3" :class="status == item.value ? 'c398DFB' :''"
							@click="switchFeedbackType(item.value)">
							{{item.label}}
						</view>
						<view v-if="status == item.value" class="underline"></view>
					</view>
				</view>
			</view>

			<!-- 空状态 -->
			<view v-if="isEmpty" class="empty-state fcolcc">
				<image class="empty-image" src="/static/images/common/empty.png"></image>
				<view class="empty-text">暂无反馈数据</view>
				<up-button type="primary" shape="circle" @click="loadFeedbackList(true)"
					style="width: 200rpx;margin-top: 30rpx;">
					重新加载
				</up-button>
			</view>

			<!-- 反馈列表 -->
			<view v-else class="p30">
				<view class="p4030 item mb20" v-for="(item,index) in feedbackList" :key="index"
					@click="goToFeedbackDetail(item.feedback_id)">
					<view class="fcsb mb12">
						<view class="f26bc3">{{item.feedback_classify_text}}</view>
						<view class="f26bcFF0C10" v-if="item.status === 0">待回复</view>
						<view class="f265c0 c398DFB" v-else>已回复</view>
					</view>
					<view class="mb25 f245c9">提交时间：{{formatDate(item.create_time)}}</view>
					<up-line></up-line>
					<view class="mt25  f245c3">{{item.feedback_content}}</view>
					<view v-if="item.status === 1" class="reply-container">
						<view class="reply-bg"></view>
						<view class="reply-content">
							<view class="f245c0">{{item.reply_content}}</view>
							<view class="mt20 f20c9">回复时间：{{formatDate(item.reply_time)}}</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 加载更多 -->
			<view v-if="isLoadingMore" class="loading-more fcc">
				<up-loading-icon text='加载中...'></up-loading-icon>
			</view>
			<view v-if="noMoreData" class="no-more fcc">
				没有更多数据了
			</view>
		</view>
	</view>
</template>

<script setup>
	import CustomNavbar from '@/components/custom-navbar.vue'
	import {
		ref,
		onMounted,
		computed
	} from 'vue';
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		myFeedback
	} from '@/api/user'

	// 系统信息
	const systemInfo = uni.getSystemInfoSync()
	const navBarHeight = computed(() => systemInfo.statusBarHeight + 44)

	// 数据状态
	const isLoading = ref(false)
	const isLoadingMore = ref(false)
	const noMoreData = ref(false)
	const isEmpty = ref(false)

	// 分页参数
	const page = ref(1)
	const limit = ref(10)
	const total = ref(0)

	// 反馈类型
	const feedbackTypes = ref([{
			label: '全部',
			value: null
		}, // 使用null而不是空字符串
		{
			label: '待回复',
			value: 0
		},
		{
			label: '已回复',
			value: 1
		}
	])
	// 初始状态设为null
	const status = ref(null)


	// 反馈列表
	const feedbackList = ref([])

	// 初始化加载
	onMounted(() => {
		loadFeedbackList(true)
	})

	// 加载反馈列表
	const loadFeedbackList = async (isRefresh = false) => {
		if (isLoading.value && !isRefresh) return

		try {
			if (isRefresh) {
				page.value = 1
				noMoreData.value = false
				isLoading.value = true
			} else {
				isLoadingMore.value = true
			}

			const params = {
				status: status.value,
				page: page.value,
				limit: limit.value
			}

			const res = await myFeedback(params)

			if (res.code === 200) {
				if (isRefresh) {
					feedbackList.value = res.data.data
					total.value = res.data.total
				} else {
					feedbackList.value = [...feedbackList.value, ...res.data.data]
				}

				// 检查是否还有更多数据
				if (res.data.data.length < limit.value) {
					noMoreData.value = true
				}

				// 检查空状态
				isEmpty.value = feedbackList.value.length === 0
			}
		} catch (error) {
			console.error('加载反馈列表失败:', error)
			uni.showToast({
				title: '加载失败，请重试',
				icon: 'none'
			})
		} finally {
			isLoading.value = false
			isLoadingMore.value = false
		}
	}

	// 切换反馈类型
	const switchFeedbackType = (type) => {
		if (status.value === type) return
		status.value = type
		loadFeedbackList(true)
	}

	// 格式化日期
	const formatDate = (dateString) => {
		if (!dateString) return ''
		const date = new Date(dateString)
		return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`
	}

	// 返回
	const handleBack = () => {
		uni.navigateBack()
	}

	// 加载更多
	onReachBottom(() => {
		if (isLoadingMore.value || noMoreData.value) return
		page.value += 1
		loadFeedbackList()
	})
	// 跳转到反馈详情页
	const goToFeedbackDetail = (feedback_id) => {

		uni.navigateTo({
			// 修正路径，去掉重复的 pages/user
			url: `/pages/user/feedbackDetails?feedback_id=${feedback_id}`
		})
	}
</script>

<style lang="scss" scoped>
	.home {
		width: 100%;
		min-height: 100vh;
		background-color: #f7f7f7;

		.content {
			padding-top: var(--status-bar-height);
			padding-top: calc(var(--status-bar-height) + 70rpx);

			.search {
				background: #FFFFFF;
				padding: 16rpx 30rpx 24rpx 30rpx;

				.typelist {
					position: relative;

					.underline {
						width: 40rpx;
						height: 4rpx;
						background: #398DFB;
						border-radius: 500rpx;
						position: absolute;
						bottom: -23rpx;
					}
				}
			}

			.empty-state {
				height: 60vh;
				padding: 40rpx;
				text-align: center;

				.empty-image {
					width: 300rpx;
					height: 300rpx;
					margin-bottom: 30rpx;
				}

				.empty-text {
					font-size: 28rpx;
					color: #999;
				}
			}

			.item {
				width: 100%;
				background: #FFFFFF;
				border-radius: 20rpx;


				.reply-container {
					position: relative;
					margin-top: 20rpx;

					.reply-bg {
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						background: #398DFB;
						border-radius: 0rpx 20rpx 20rpx 20rpx;
						opacity: 0.1;
					}

					.reply-content {
						position: relative;
						padding: 20rpx 30rpx;
						z-index: 1;
					}
				}

				.f26bc3 {
					font-size: 26rpx;
					font-weight: bold;
					color: #333;
				}

				.f26bcFF0C10 {
					font-size: 26rpx;
					font-weight: bold;
					color: #FF0C10;
				}

				.f245c9 {
					font-size: 24rpx;
					color: #999;
				}

				.f245c3 {
					font-size: 24rpx;
					color: #5c5c5c;
				}
			}

			.loading-more,
			.no-more {
				padding: 30rpx 0;
				color: #999;
				font-size: 26rpx;
			}
		}
	}

	.flex {
		display: flex;
	}

	.fcolcc {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.fcsb {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.p0030 {
		padding: 0 30rpx;
	}

	.p30 {
		padding: 30rpx;
	}

	.p4030 {
		padding: 40rpx 30rpx;
	}

	.mb12 {
		margin-bottom: 12rpx;
	}

	.mb20 {
		margin-bottom: 20rpx;
	}

	.mb25 {
		margin-bottom: 25rpx;
	}

	.mt20 {
		margin-top: 20rpx;
	}

	.mt25 {
		margin-top: 25rpx;
	}

	.f30bc3 {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
	}

	.c398DFB {
		color: #398DFB;
	}
</style>